package com.hooper.project.rpc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hooper.api.common.model.UserInfo;
import com.hooper.api.common.service.UserService4Dubbo;
import com.hooper.project.common.ErrorCode;
import com.hooper.project.exception.BusinessException;
import com.hooper.project.model.entity.User;
import com.hooper.project.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/04/12/23:41
 */
@DubboService(timeout = 2000)
@Component
public class UserService4DubboImpl implements UserService4Dubbo {

    @Resource
    private UserService userService;
    @Override
    public UserInfo getUserInfoByAccessKey(String accessKey) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAccessKey, accessKey);
        User user = userService.getOne(wrapper);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
        return userInfo;
    }
}
